package com.sky.controller.admin;

import com.aliyuncs.ecs.model.v20140526.AttachKeyPairResponse;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 文件上传
     */
    public Result uploadFile(MultipartFile file) throws Exception{
        //1获取上传的原始文件
        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));

        //2生成UUID
        String uuidName = UUID.randomUUID().toString() + substring;
        //3获取文件的字节数组
        byte[] bytes = file.getBytes();
        //4将文件上传
        String imgUrl = aliOssUtil.upload(bytes, uuidName);
        return Result.success(imgUrl);
    }


}
